package com.qf.voteconsumer.consumer;

import com.qf.voteconsumer.vote.service.IVoteService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class VoteQueueConsumer {

    @Autowired
    IVoteService voteService;

    @RabbitListener(queues = "vote-queue")
    public void getMsg(String json, Message message, Channel channel) throws Exception {

        log.info("收到的消息内容为{}",json);

        // 调用service层对象，对消息进行具体处理
        voteService.processMsg(json);

        // 手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }

}
